yjx.controller('productDetailController', ['$rootScope', '$window', '$scope', '$stateParams', '$state', '$fetch', 'LoginService', 'modalService', '$timeout', 'wxShareService', '$location', '$locals','browserJudge', function ($rootScope, $window, $scope, $stateParams, $state, $fetch, LoginService, modalService, $timeout, wxShareService, $location, $locals,browserJudge) {
    $scope.nodata = false;
    var id = $stateParams.id,
        pendingEventId = $stateParams.pendingEventId,
        url=$stateParams.pdfUrl;
    $scope.forwardId = $stateParams.forwardId;
    $rootScope.tabCtrl.tab1 = true;
    $rootScope.tabCtrlName = $rootScope.tabCtrlNameArr[1]
    /*外部浏览器中打开或下载pdf*/
    if(url && !browserJudge.isWx()){
        window.location.href=ALISRC+url
    }

    /*转发成功后的回调*/
    $rootScope.wxShareSuc = function () {
        /*完成待办事项*/
        //alert($rootScope.identity )
        if ($rootScope.identity == 1 && pendingEventId) {
            //alert(pendingEventId)
            $fetch.fetch(
                '/share/recordRelay/content/times',
                {
                    params: {
                        userId: $rootScope.userId,
                        contentId: id,
                        pendingEventId: pendingEventId,
                        type: 2
                    },
                    cache: false,
                    //type:'POST'
                }
            ).then(function (data) {
                }).catch(function (e) {
                    modalService.mainMsgError(e.msg)
                })
        }
    }
    $scope.order = function (isPay) {

        if ($rootScope.identity == 1) {
            if ($scope.orderTag) {
                /*弹出转发框 提示转发给客户*/
                modalService.mainMsgInfo("点击右上角将下单链接转发给客户")
            } else {
                $scope.orderTag = true;
            }
        } else {
            if ($scope.forward) {
                $state.go('order', {
                    id: id,
                    forwardId: $scope.forward.userId,
                    forwardName: $scope.forward.nickname,
                    pendingEventId: pendingEventId
                })
            } else {
                $state.go('order', {id: id})
            }
        }
    }
    $scope.orderTag = false;

    /*根据id获取产品详情*/
    function getDetail() {
        $fetch.fetch(
            '/productCenter/productDetail',
            {
                params: {
                    productId: id
                },
                cache:false,
            }
        ).then(function (data) {
                $timeout(function () {
                    if (data && data.product) {
                        $scope.productDetail = data.product;
                        /*获取微信转发配置*/
                        //$rootScope.wxTitle = $scope.productDetail.title;
                        $rootScope.wxDesc = $scope.productDetail.title;
                        //$rootScope.wxImg = $scope.productDetail.avatar;
                        /*android分享链接添加#时自带参数会被微信自动添加的form等参数截掉  原因未知*/
                        if ($rootScope.isAndroid) {
                            var linkhead = "http://www.genetuan.com/"
                        } else {
                            var linkhead = "http://www.genetuan.com/#/"
                        }
                        if ($rootScope.identity == 1) {
                            if ($rootScope.isAndroid) {
                                $rootScope.link = linkhead + "?url=productDetail&id=" + id + "&forwardId=" + $rootScope.userId + "&pendingEventId=" + pendingEventId;
                                wxShareService.getConfig("", ['onMenuShareAppMessage', 'onMenuShareTimeline']);
                            } else {
                                $rootScope.link = linkhead + "productDetail?id=" + id + "&forwardId=" + $rootScope.userId + "&pendingEventId=" + pendingEventId;
                                wxShareService.getConfig("", ['onMenuShareAppMessage', 'onMenuShareTimeline']);
                            }
                            //$location.search({'id':id,'forwardId':$rootScope.userId,'pendingEventId':pendingEventId});/*将url改变成为要转发出去的url */
                        } else {
                            if ($rootScope.isAndroid) {
                                $rootScope.link = linkhead + "?url=productDetail&id=" + id;
                                wxShareService.getConfig("", ['onMenuShareAppMessage', 'onMenuShareTimeline']);
                            } else {
                                $rootScope.link = linkhead + "productDetail?id=" + id;
                                wxShareService.getConfig("", ['onMenuShareAppMessage', 'onMenuShareTimeline']);
                            }
                        }
                    } else {
                        $scope.nodata = true;
                    }
                })
            }).catch(function (e) {
                modalService.mainMsgError(e.msg)
            })
    }
/* && browserJudge.isWx()*/
    if (id && browserJudge.isWx() ) {
        getDetail();
    }

    /*获取转发人信息*/
    if ($scope.forwardId) {
        $fetch.fetch(
            '/user/getBusinessCard',
            {
                params: {
                    id: $scope.forwardId
                }
            }
        ).then(function (data) {
                $timeout(function () {
                    if (data && data.businessCard) {
                        $scope.forward = data.businessCard;
                        console.log($scope.forward);
                    }
                })
            }).catch(function (e) {
            })
    }

    $scope.dldPdf= function(url){
        modalService.mainMsgInfo("点击右上角选择从浏览器打开");
        url=url.split(ALISRC);
        var url2 = url[1];
        if(url2){
            $location.search('pdfUrl',url2).replace();
        }
    }

}])